package com.hlxl.viewpointrest.controller;

import com.hlxl.viewpoint.model.ViewPoint;
import com.hlxl.viewpointrest.dto.Page;
import com.hlxl.viewpointrest.dto.ViewPointDTO;
import com.hlxl.viewpointrest.service.ViewPointService;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wyy on 18-5-4
 */
@RestController()
@RequestMapping("/api")
public class ApiController {

    @Autowired
    ViewPointService viewPointService;

    @ApiOperation(value = "添加视点", httpMethod = "POST")
    @PostMapping("/view-point")
    public ViewPointDTO addViewPoint(@RequestBody @ApiParam(value = "视点信息，不含id", required = true) ViewPointDTO viewPointDTO) throws SystemException {
        //todo check no id
        ViewPoint viewPoint = viewPointService.addViewPoint(viewPointDTO);
        viewPointDTO.setId(viewPoint.getId());
        return viewPointDTO;
    }

    @ApiOperation(value = "删除视点", httpMethod = "DELETE")
    @DeleteMapping("/view-point/{id}")
    public void deleteViewPoint(@ApiParam(value = "视点的id", required = true) @PathVariable long id) throws PortalException, SystemException {
        viewPointService.deleteViewPont(id);
    }


    @ApiOperation(value = "修改视点", httpMethod = "PUT")
    @PutMapping("/view-point")
    public ViewPointDTO updateViewPoint(@RequestBody @ApiParam(value = "视点信息,含id", required = true) ViewPointDTO viewPointDTO) throws PortalException, SystemException {
        viewPointService.updateViewPoint(viewPointDTO);
        return viewPointDTO;
    }

    @ApiOperation(value = "获取所有视点", httpMethod = "GET")
    @GetMapping("/view-points")
    public List<ViewPointDTO> viewpoints(@ApiParam(value = "视点的name", defaultValue = "") @RequestParam String name,
                                         @ApiParam(value = "分页参数") Page page) throws SystemException {
        List<ViewPoint> viewPointList = viewPointService.getViewPoints(name, page);
        List<ViewPointDTO> result = viewPointList.stream().map(viewPoint -> new ViewPointDTO(viewPoint.getId(), viewPoint.getName())).collect(Collectors.toList());

        return result;
    }

    @ApiOperation(value = "获取视点的信息", httpMethod = "GET")
    @GetMapping("/view-point/{id}")
    public ViewPointDTO getViewPoint(@ApiParam(value = "视点的id", required = true) @PathVariable long id) throws PortalException, SystemException {
        ViewPoint viewPoint = viewPointService.getViewPoint(id);
        return new ViewPointDTO(viewPoint.getId(), viewPoint.getName());
    }

    @GetMapping("/check")
    public String check(HttpServletRequest request) {
        String screenName = request.getRemoteUser();
        return "check";
    }

    @GetMapping("/no-check")
    public String noCheck() {
        return "no check";
    }

    @GetMapping("/security-exception")
    public String securityException() {
        if (true) {
            throw new SecurityException("no permission message!");
        }
        return "success";
    }


}
